import Request from 'luch-request';

const http = new Request({
    baseURL: import.meta.env.VITE_BASE_URL, //设置请求的base url
    timeout: 30000, //超时时长30秒,
    header: {
        'Content-Type': 'application/json'
    }
});

//请求拦截器
http.interceptors.request.use(
    (config) => {
        return config;
    },
    (error) => {
        return Promise.resolve(error);
    }
);

// 响应拦截器
http.interceptors.response.use(
    (response) => {
        const {config, data} = response;
        // 满足条件时自动提示
        if (data?.code !== 'SUCCESS') {
            console.error('请求异常', data);
            if (config.custom?.showErrorToast != false) {
                uni.showToast({title: data?.message || "服务异常", icon: 'none', duration: 3000})
            }
        }
        return data;
    },
    (error) => {
        console.error('请求异常', error);
        uni.showToast({title: error.errMsg, icon: 'none', duration: 3000});
        return Promise.resolve(error);
    }
);
export default http;
